package com.base.study.fragment;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.study.R;
import com.base.study.bean.SubjectListBean;
import com.base.study.databinding.FragmentTestPaperBinding;
import com.base.study.presenter.TestPaperPresenter;
import com.base.study.view.TestPaperView;
import com.base.study.widget.RecyclerViewSpacesItemDecoration;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weikailiang on 2020/3/13.
 */

@SuppressLint("ValidFragment")
public class TestPaperFragment extends CBaseMvpFragment<TestPaperPresenter> implements TestPaperView{
    private List<SubjectListBean.Rows> mData = new ArrayList<>();
    private BaseAdapter<SubjectListBean.Rows> mAdapter;
    private FragmentTestPaperBinding mBinding;
    private String id;
    private int page = 1;
    @Override
    public void init() {

        mBinding = (FragmentTestPaperBinding) mRootBinding;
        mPresenter = new TestPaperPresenter(this,_mActivity);
        mPresenter.start();
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
    }


    @Override
    public void start() {
        mAdapter = new BaseAdapter<SubjectListBean.Rows>(mData, mContext, true) {

            @Override
            public void convert(BaseHolder holder, final SubjectListBean.Rows subjectListBean, boolean isHeader, boolean isBottom) {
                TextView title = holder.getView(R.id.text1);
                TextView type = holder.getView(R.id.text_type);
                TextView text3 = holder.getView(R.id.text3);
                TextView text4 = holder.getView(R.id.text4);
                TextView textDate = holder.getView(R.id.text_date);
                title.setText(subjectListBean.getItemName());
                type.setText(subjectListBean.getQuestionsType());
                text3.setText(subjectListBean.getRootSubjectName());
                text4.setText(subjectListBean.getSubjectName());

                StringBuilder stringBuilder = null;

                if ("单选题".equals(subjectListBean.getQuestionsType())) {
                    stringBuilder = new StringBuilder();
//                    stringBuilder.append(subjectListBean.getPickFruits());
//                    stringBuilder.append("：");
                    if ("A".equals(subjectListBean.getPickFruits())) {
                        stringBuilder.append(subjectListBean.getAnswerOne());
                    }
                    if ("B".equals(subjectListBean.getPickFruits())) {
                        stringBuilder.append(subjectListBean.getAnswerTwo());
                    }
                    if ("C".equals(subjectListBean.getPickFruits())) {
                        stringBuilder.append(subjectListBean.getAnswerThree());
                    }
                    if ("D".equals(subjectListBean.getPickFruits())) {
                        stringBuilder.append(subjectListBean.getAnswerFour());
                    }
                    if ("E".equals(subjectListBean.getPickFruits())) {
                        stringBuilder.append(subjectListBean.getAnswerFive());
                    }
                    if ("F".equals(subjectListBean.getPickFruits())) {
                        stringBuilder.append(subjectListBean.getAnswerSix());
                    }
                    textDate.setText(stringBuilder.toString());
                    //textDate.setText(subjectListBean.getJudgePickFruits()+subjectListBean.getAnswerTwo());
                }
//                if ("单选题".equals(subjectListBean.getQuestionsType())) {
//                    textDate.setText("参考答案：" + subjectListBean.getPickFruits());
//                }
                else if ("多选题".equals(subjectListBean.getQuestionsType())) {
                    stringBuilder = new StringBuilder();

                    if (subjectListBean.getMultiSelectFruits().contains("A")) {
                        stringBuilder.append(subjectListBean.getAnswerOne());
                        stringBuilder.append("\n");

                    }
                    if (subjectListBean.getMultiSelectFruits().contains("B")) {
                        stringBuilder.append(subjectListBean.getAnswerTwo());
                        stringBuilder.append("\n");

                    }
                    if (subjectListBean.getMultiSelectFruits().contains("C")) {
                        stringBuilder.append(subjectListBean.getAnswerThree());
                        stringBuilder.append("\n");

                    }
                    if (subjectListBean.getMultiSelectFruits().contains("D")) {
                        stringBuilder.append(subjectListBean.getAnswerFour());
                        stringBuilder.append("\n");


                    }
                    if (subjectListBean.getMultiSelectFruits().contains("E")) {
                        stringBuilder.append(subjectListBean.getAnswerFive());
                        stringBuilder.append("\n");

                    }
                    if (subjectListBean.getMultiSelectFruits().contains("F")) {
                        stringBuilder.append(subjectListBean.getAnswerSix());

                    }
                    textDate.setText(stringBuilder.toString());
                }
                if ("判断题".equals(subjectListBean.getQuestionsType())) {
                    textDate.setText(subjectListBean.getJudgePickFruits());
                }

            }
        };
        mAdapter.putNormalType(R.layout.item_test_paper_type_list);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                SubjectListBean.Rows rows = mData.get(dataPosition);
                ARouter.getInstance().build("/subject/test_typedetail")
                        .withString("rows", new Gson().toJson(rows))
                        .navigation();
            }
        });
        mBinding.rvTestTypeList.setAdapter(mAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rvTestTypeList.setLayoutManager(linearLayoutManager);
        mBinding.rvTestTypeList.addItemDecoration(new RecyclerViewSpacesItemDecoration
                (10, 10, 10, 10));
        mBinding.smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                if (getArguments() != null) {
                    id = getArguments().getString("id");
                }
                mPresenter.getList(page, 10, id);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                if (getArguments() != null) {
                    id = getArguments().getString("id");
                }
                mPresenter.getList(page, 10,id);
            }
        });
    }

//    @SuppressLint("ValidFragment")
//    public TestPaperFragment(String id){
//        this.id = id;
//    }
//    public static TestPaperFragment newInstance(String resourId) {
//        return new TestPaperFragment(resourId);
//    }

    @Override
    protected void lazyLoad() {
        if (mBinding.smartRefreshLayout!=null) {
            mBinding.smartRefreshLayout.autoRefresh();
        }
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_test_paper;
    }

    @Override
    public void returnError() {
    }

    @Override
    public void returnList(SubjectListBean subjectListBean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.clear();
        mData.addAll(subjectListBean.getRows());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnMoreList(SubjectListBean subjectListBean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.addAll(subjectListBean
                .getRows());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnNoMoreData() {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(true);
    }

    @Override
    public void returnNoData() {
        mData.clear();
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.empty.setVisibility(View.VISIBLE);
    }

    @Override
    public void returnListError() {
        mBinding.empty.setVisibility(View.GONE);
        if (page == 1) {
            mBinding.smartRefreshLayout.finishRefresh();
        } else {
            mBinding.smartRefreshLayout.finishLoadmore();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }
}
